#<A name="xxx"></A>

import os.path


z=z1=z2=''
tree=[]

row=[]
tab=[]
tabs=[]
stabs=[]
f = open('bookmarks.html', 'r')
tSpace = 0
oldtSpace = 0
"""
class MyTab:

    maxCol = 0
    oldmaxCol = 0
    tabName = ''
    tab = []
    def reset(self):
    	maxCol = 0
    	oldmaxCol = 0
	tabName = ''
	tab = []

x = MyTab()
"""
maxCol = 0
oldmaxCol = 0
tabName = ''

class MyTab(object):

	"""__init__() functions as the class constructor"""

	def __init__(self, tabName=None, maxCol=None, tab=None):

		self.tabName = tabName

		self.maxCol = maxCol

		self.tab = tab

MyTabList=[]

def fx(linea,stringa,inizio,fine):
  #fx(line,s,0,len(line))
  ls= len(stringa)+inizio
  z=linea[ls:fine].find(stringa)
  if z!=-1:
	return z

def treeSpace(line):
	global tSpace,oldtSpace
	ssZero = '<DL>'
	zzZero = line.find(ssZero) 
	if zzZero != -1:
		tSpace += 1
	ssOne = '</DL>'
	zzOne = line.find(ssOne) 
	if zzOne != -1:
	  if tSpace >0:
		tSpace -= 1
		if tree:
		 tree.pop()

def treeFolders(line):
	global tSpace,oldtSpace
	ssZero = '<DT><H3'
	zzZero = line.find(ssZero) 
	if zzZero != -1:
		lssZero = zzZero + len(ssZero)

		ssOne = '>'
		zzOne = line[lssZero:].find(ssOne)
		if zzOne != -1:
			lssOne = lssZero + zzOne +  len(ssOne)

			ssTwo = '<'
			zzTwo = line[lssOne:].find(ssTwo)
			if zzTwo != -1:
				lssTwo = lssOne + zzTwo #+ len(ssTwo)
				#print '<TH>'+'<A>' + line[lssOne:lssTwo]+'</A>'+'</TH>'
				tree.append(line[lssOne:lssTwo])
				#for t in tree:
					#print t
				#print tSpace
			
def treeLine(line):
  global row, tab, maxCol,oldmaxCol
  s='<DT><A HREF=\"'
  ls= len(s)
  z = line.find(s)
  if z != -1:
  	ls= z+len(s)

	sOne='\"'
	z1=line[ls:].find(sOne)
	if z1 != -1:
		lsOne = ls+z1#+len(sOne)
		#print '<TR>'+'<TD HEIGHT=17 ALIGN=LEFT>'
		#print '<A HREF=\"' +line[ls:lsOne] + '\">'
		row.append(line[ls:lsOne])#url
		lsTwo = lsOne + len(sOne)

		sTwo = '>'
		z2 = line[lsTwo:].find(sTwo)
		if z2!=-1:
			lsThree = lsTwo+z2+len(sTwo)
			#print z,z1,z2,lsThree
			
			sThree='</A>'
			z3=line[lsThree:].find(sThree)
			if z3!=-1:
				lsFour = lsThree + z3 #+ len(sThree)
				#tSpaceTwo = '   '*tSpace 
				#print tSpaceTwo,line[lsThree:lsFour] +'</A>'+'</TD>'+'</TR>'
				l = line[lsThree:lsFour]
				if l!='':
					row.append(l)#description
				else:
					row.append('empty')
				
  				if len(tree)!=0:	#categorie = bkm folders 
   					for t in range(0,len(tree)):
     						row.append(tree[t])
				tab.append(row)
				#print 'len riga: ' + str(len(row))
				maxCol = len(row)
				if maxCol > oldmaxCol:
					oldmaxCol = maxCol
				else:
					maxCol=oldmaxCol
				row=[]			

def htmlparse():
 global row, tab, tabName, tSpace

 while 1:
  line=f.readline()
  if not line: break
  #print line  
  treeFolders(line)
  treeSpace(line)
  treeLine(line)

  if len(tree)==0:
    if len(tab)!=0:
	for t in tab:
  	 	for r in range(len(t)):
   			if r==2:
				#print 'nome tabella: ',t[r]
				tabName = t[r]
	#print 'maxCol: ' + str(maxCol)

	MyTabList.append(MyTab(tabName,maxCol,tab))
	#x.tab=tab
	#stabs.append(x)
	#x.reset()
	#tabs.append(tab)
	tab = []

    


 f.close()


htmlparse()
"""
for tt in MyTabList:
 #for t in tt:
	print '\n'
	print tt.tabName
	print tt.maxCol
	for t in tt.tab:
 		print 'divisore\n'	
  		for r in range(len(t)):
	    		print t[r]

"""
"""
for tt in tabs:
 for t in tt:
  print '\n'
  for r in range(len(t)):
   if r==2:
	print 'nome tabella: ',t[r]
   else:
    print t[r]
 print 'divisore\n'
"""
